import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/club/req_club.dart';
import 'package:flutter_changdian_football/api/req/other/req_other.dart';
import 'package:flutter_changdian_football/config/enum/load_state.dart';
import 'package:flutter_changdian_football/model/club/club_detail_model.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/custom_toast.dart';
import 'package:flutter_changdian_football/utils/share_util.dart';
import 'package:flutter_changdian_football/widget/dialog/operational_results_dialog.dart';
import 'package:flutter_changdian_football/widget/sheet/share_icon_sheet.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class ClubRankingDetailsController extends GetxController {

  ClubRankingDetailsController({
    required this.clubId
  });

  final String clubId;

  @override
  void onInit() {
    super.onInit();

    initData();
  }

  ClubDetailModel? clubDetailModel;

  LoadState state = LoadState.loading;

  initData() async {
    var resp = await ReqClub.clubDetail(clubId: clubId);
    if (resp.status == Status.completed) {
      if (resp.data['code'] == '200') {
        clubDetailModel = ClubDetailModel.fromJson(resp.data['data']);

        if (resp.data['data'] != null) {
          state = LoadState.success;
        } else {
          state = LoadState.empty;
        }

        update();
      } else {
        CustomToast.showTextToast('${resp.data['msg']}');
        state = LoadState.error;
        update();
      }
    } else {
      state = LoadState.network;
      update();
    }
  }


  /// 分享
  getSharePage() async {
    try {
      EasyLoading.show();
      var resp = await ReqOther.getSharePage(sharePageName: CustomConstant.SHARE_CLUB_CENTER, clubId:'${clubId}');

      if (resp.data['code'] == '200') {
        // 分享推荐
        ShareIconSheet.shareIcon(
          null, 
          null, 
          resp.data['data']['url'], 
          null,
          isShowImg: false,
          wxTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "0");
          },
          pyqTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "1");
          },
          qqTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "2");
          },
          wbTap: () {
            ShareUtil.shareUrl(title: resp.data['data']['sharePageName'], description: resp.data['data']['shareCardDescription'], image: resp.data['data']['shareCardImageUrl'],  url: resp.data['data']['shareUrl'], shareType: "3");
          },
        );
      } else {
        CustomToast.showTextToast(resp.data['msg']);
      }
    } catch (e) {
      CustomToast.showTextToast('系统繁忙，请稍后再试！');
    } finally {
      EasyLoading.dismiss();
    }
  }


  TextEditingController messageController = TextEditingController();

  /// 申请加入俱乐部
  joinApply() async {
    if (messageController.text.trim() == '') {
      CustomToast.showTextToast('请简述您的个人信息，以便球队负责人审核');
      return ;
    }

    try {
      Get.back();

      EasyLoading.show();

      var resp = await ReqClub.joinApply(
        clubId: '$clubId',
        message: '${messageController.text}'
      );
      if (resp.status == Status.completed) {
        if (resp.data['code'] == '200') {
          OperationalResultsDialog.show(
            Get.context!,
            text: '加入申请提交成功',
            icon: 'assets/images/img_tc_cg.png',
            confirmTap: () {
              Get.back();
            }
          );
        } else {
          OperationalResultsDialog.show(
            Get.context!,
            text: resp.data['msg'] ?? '系统繁忙，请稍后再试！',
            icon: 'assets/images/img_tc_sb.png',
            confirmTap: () {
              Get.back();
            }
          );
        }
      } else {
        OperationalResultsDialog.show(
          Get.context!,
          text: '系统繁忙，请稍后再试！',
          icon: 'assets/images/img_tc_sb.png',
          confirmTap: () {
            Get.back();
          }
        );
      }
    } catch (e) {
      OperationalResultsDialog.show(
        Get.context!,
        text: '系统繁忙，请稍后再试！',
        icon: 'assets/images/img_tc_sb.png',
        confirmTap: () {
          Get.back();
        }
      );
    } finally {
      EasyLoading.dismiss();
    }
  }

}